package com.zxp.mapstruct.convert;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.zxp.mapstruct.dto.StudentDTO;
import com.zxp.mapstruct.enums.LevelEnum;
import com.zxp.mapstruct.po.Student;
import org.mapstruct.*;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 定义这是一个MapStruct对象属性转换接口，在这个类里面规定转换规则
 */
@Mapper(imports = {StudentMapping.class, LevelEnum.class})
public interface  StudentConvert {
    @Mappings({
        @Mapping(source = "student.id", target = "studentId"),
        @Mapping(source = "student.name", target = "studentName"),
        @Mapping(source = "student.age", target = "age"),
        @Mapping(target = "level", expression = "java( getDesc(student.getLevel()) )"),
        @Mapping(target = "ageLevel", expression = "java(StudentMapping.ageLevel(student.getAge()))"),

        @Mapping(target = "student.sexName", expression = "java(StudentMapping.sexName(student.getSex()))"),
        @Mapping(source = "student.admissionTime", target = "admissionDate", dateFormat = "yyyy-MM-dd")
    })
    StudentDTO from(Student student);

    default LocalDate map(LocalDateTime time) {
        return time.toLocalDate();
    }

    default LevelEnum getByCode(Integer code){
        return LevelEnum.getByCode(code);
    }

    default String getDesc(LevelEnum levelEnum) {
        LevelEnum levelEnum2  = ObjectUtil.isEmpty(levelEnum) ? LevelEnum.able: levelEnum;
        return  levelEnum2.getDesc();
    }

}
